//
//  BuyMemberViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/20.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "BuyMemberViewController.h"
#import "VipClassModel.h"
#import <AlipaySDK/AlipaySDK.h>
@interface BuyMemberViewController ()

@property (strong, nonatomic) UIView *userView;
@property (strong, nonatomic) UIImageView *iconIV;
@property (strong, nonatomic) UILabel *userNameL;
@property (strong, nonatomic) UIView *memberDeslView;
@property (strong, nonatomic) UILabel *levelL;
@property (strong, nonatomic) UILabel *desL2;
@property (strong, nonatomic) UILabel *desL3;
@property (strong, nonatomic) UIScrollView *buyMemberView;
@property (strong, nonatomic) NSMutableArray *imageBgArr;
@property (strong, nonatomic) NSMutableArray *moneyArr;

@property (nonatomic, strong) NSArray *vipClassArray;
// 被选中的会员价格
@property (copy, nonatomic) NSString *selectedMemberMoney;

@property (nonatomic, assign) NSInteger selectIndex;

@end

@implementation BuyMemberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = WhiteColor;
    self.title = @"爱淘帮会员购买";
    
    [self createUserView];
    [self createMemberDesView];
    [self createBuyMemberView];
    [self createOtherView];
    
    NSString *userId = [NSString stringWithFormat:@"{\"user_id\" : \"%@\"}",[RSA encryptString:[CommonUserInfo sharedInstance].user_id publicKey:PublicKey]];
    
    WS(weakSelf);
    [PPNetworkHelper POST:@"user/vip/viplist" parameters:@{@"json":userId} success:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 1) {
            weakSelf.vipClassArray = [VipClassModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [weakSelf reloadDataVipClass:weakSelf.vipClassArray];
        }
    } failure:^(NSError *error) {
    }];
}

- (void)reloadDataVipClass:(NSArray *)array {
    
    for (int i = 0 ; i < array.count; i ++) {
        VipClassModel * model = array[i];
        [self createViewWithParentView:self.buyMemberView andTag:i withLevel:[NSString stringWithFormat:@"爱淘帮超级会员V%@（%@）",model.grade,model.day==30?@"月卡":@"年卡"] money:model.price OldPrice:model.old_price];
    }
  self.buyMemberView.contentSize = CGSizeMake(self.view.size.width, jisuan(90)*array.count);
    
}


#pragma mark -选择会员
- (void)viewGestureClick:(UIButton *)button {
    
    self.selectIndex = button.tag;
    if (button.selected) {
        
    } else {
        button.selected = YES;
        for (int i = 0; i < self.imageBgArr.count; i ++) {
            UIButton *button = self.imageBgArr[i];
            if (button.tag!=self.selectIndex) {
                button.selected = NO;
            }
        }
    }
}

#pragma mark -支付
- (void)payGestureClick:(UIGestureRecognizer *)recognizer {
    
    
    VipClassModel *model = self.vipClassArray[self.selectIndex];
     NSString *userPar = [NSString stringWithFormat:@"{\"userid\" : \"%@\",\"vip_id\" : \"%@\"}",[RSA encryptString:[CommonUserInfo sharedInstance].user_id publicKey:PublicKey], [RSA encryptString:model.vipId publicKey:PublicKey]];
    
    [PPNetworkHelper POST:@"pay/Pay/index" parameters:@{@"json":userPar} success:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 1) {
            //应用注册scheme,在AliSDKDemo-Info.plist定义URL types
//            NSString *appScheme = @"qxt2018082961163803";
            NSString *appScheme = AliPayAppid;
            
            // NOTE: 调用支付结果开始支付
            [[AlipaySDK defaultService] payOrder:responseObject[@"data"] fromScheme:appScheme callback:^(NSDictionary *resultDic) {
                NSLog(@"reslut = %@",resultDic);
                //[[KYAlertView sharedInstance] showAlertViewWithMessage:@"支付成功，请去我的订单查看"];
                
            }];
        } else {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        }
    } failure:^(NSError *error) {
        
    }];
    
}

#pragma mark -趣享淘会员-用户协议
- (void)tipClick:(UIGestureRecognizer *)recognizer {
    WebViewController *vc = [[WebViewController alloc] init];
    vc.title = @"用户协议";
    vc.url = @"http://www.quxiangtao.com/H5/invitatRegist/agreement.html";
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)createOtherView {
    
    UILabel *tipL = [[UILabel alloc] init];
    tipL.textColor = Color999999;
    tipL.font = Font(20);
    tipL.textAlignment = NSTextAlignmentLeft;
    tipL.numberOfLines = 0;
    [self.view addSubview:tipL];
    
    __weak typeof(self) weakSelf = self;
    [tipL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(60));
        make.top.mas_equalTo(weakSelf.buyMemberView.mas_bottom).with.offset(jisuan(10));
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(60));
        
    }];
    
    UIImageView *payIV = [[UIImageView alloc] init];
    payIV.contentMode = UIViewContentModeScaleAspectFit;
    payIV.image = [UIImage imageNamed:@"buyMemberPayBG"];
    payIV.userInteractionEnabled = YES;
    [self.view addSubview:payIV];
    [payIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
        make.top.mas_equalTo(tipL.mas_bottom).with.offset(jisuan(50));
        make.width.mas_equalTo(jisuan(550));
        make.height.mas_equalTo(jisuan(84));
    }];
    UITapGestureRecognizer *payGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(payGestureClick:)];
    [payIV addGestureRecognizer:payGesture];
    
    UILabel *payL = [[UILabel alloc] init];
    payL.text = @"立即支付";
    payL.textColor = WhiteColor;
    payL.font = Font(32);
    payL.textAlignment = NSTextAlignmentLeft;
    
    [self.view addSubview:payL];
    [payL mas_makeConstraints:^(MASConstraintMaker *make) {
        
      make.centerX.mas_equalTo(payIV.mas_centerX);
        make.centerY.mas_equalTo(payIV.mas_centerY);
        
    }];
    UILabel *tip1 = [[UILabel alloc] init];
    NSString *str = @"支付即视为同意《爱淘帮会员-用户协议》";
    tip1.userInteractionEnabled = YES;
    UITapGestureRecognizer *tipGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tipClick:)];
    [tip1 addGestureRecognizer:tipGesture];
    
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:str];
    [attributeStr addAttribute:NSForegroundColorAttributeName value:Color999999 range:NSMakeRange(0, 7)];
    [attributeStr addAttribute:NSForegroundColorAttributeName value:Color1f89ff range:NSMakeRange(7, str.length - 7)];
    tip1.attributedText = attributeStr;
    
    tip1.font = Font(20);
    tip1.textAlignment = NSTextAlignmentLeft;
    
    [self.view addSubview:tip1];
    [tip1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
        make.top.mas_equalTo(payIV.mas_bottom).with.offset(jisuan(60))
        ;
    }];
    
}

- (void)createBuyMemberView {
    
    __weak typeof(self) weakSelf = self;
    UIScrollView *buyMemberView = [[UIScrollView alloc] init];
    self.buyMemberView = buyMemberView;
    [self.view addSubview:buyMemberView];
    [buyMemberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.memberDeslView.mas_bottom);
        make.height.mas_equalTo(jisuan(390));
    }];
    

}

- (void)createViewWithParentView:(UIScrollView *)parentView andTag:(NSInteger)tag withLevel:(NSString *)level money:(NSString *)money OldPrice:(NSString *)oldPrice{
    
    UIButton *button = [[UIButton alloc] init];
    button.tag = tag;
    [button setBackgroundImage:[UIImage imageNamed:@"buyMemberViewSelectedbg"] forState:UIControlStateSelected];
    [button setBackgroundImage:[UIImage imageNamed:@"buyMemberViewNotSelectedBG"] forState:UIControlStateNormal];
    [parentView addSubview:button];
    [button addTarget:self action:@selector(viewGestureClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.imageBgArr addObject:button];

    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_offset(tag*jisuan(100));
        make.width.mas_equalTo(jisuan(630));
        make.height.mas_equalTo(jisuan(80));
    }];
    
    UILabel *levelL = [[UILabel alloc] init];
    levelL.text = level;
    levelL.textColor = Color333333;
    levelL.font = Font(24);
    levelL.textAlignment = NSTextAlignmentLeft;
    [button addSubview:levelL];
    [levelL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(button.mas_left).with.offset(Margin);
        make.centerY.mas_equalTo(button.mas_centerY);
    }];
    
    UILabel *moneyL = [[UILabel alloc] init];
    moneyL.text = [NSString stringWithFormat:@"￥%@",money];
    moneyL.textColor = Color333333;
    moneyL.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    moneyL.textAlignment = NSTextAlignmentLeft;
    if (tag == 0) {
        button.selected = YES;
        self.selectIndex = button.tag;
    }
    [button addSubview:moneyL];
    [moneyL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(button.mas_right).with.offset(-42);
        make.centerY.mas_equalTo(button.mas_centerY).offset([money floatValue] == [oldPrice floatValue] ? 0 : -4);
    }];
    
    UILabel *xianshiLabel = [[UILabel alloc] init];
    xianshiLabel.text = @"限时";
    xianshiLabel.textColor = [UIColor whiteColor];
    xianshiLabel.font = Font(12);
    xianshiLabel.textAlignment = NSTextAlignmentCenter;
    xianshiLabel.backgroundColor = [UIColor redColor];
    [button addSubview:xianshiLabel];
    [xianshiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.equalTo(moneyL.mas_centerY);
        make.height.mas_equalTo(10);
        make.width.mas_equalTo(20);
    }];
    MKViewBorderRadius(xianshiLabel, 5, 0, [UIColor whiteColor]);
    
    xianshiLabel.hidden = [money floatValue] == [oldPrice floatValue] ? YES : NO;
    
    UILabel *yuanjiaLabel = [[UILabel alloc] init];
    yuanjiaLabel.text = [NSString stringWithFormat:@"原价￥%@",oldPrice];
    yuanjiaLabel.font = [UIFont systemFontOfSize:jisuan(16)];
    yuanjiaLabel.textColor = Color999999;
    [button addSubview:yuanjiaLabel];
    [yuanjiaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.top.equalTo(button.mas_centerY).offset(4);
    }];
    yuanjiaLabel.hidden = [money floatValue] == [oldPrice floatValue] ? YES : NO;
    
//    UIImageView *moneyLogo = [[UIImageView alloc] init];
//    moneyLogo.contentMode = UIViewContentModeScaleAspectFit;
//    moneyLogo.image = [UIImage imageNamed:@"moneyLogoBG"];
//    [button addSubview:moneyLogo];
//    [moneyLogo mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(moneyL.mas_left).with.offset(-jisuan(10));
//        make.centerY.mas_equalTo(button.mas_centerY);
//        make.width.mas_equalTo(jisuan(14));
//        make.height.mas_equalTo(jisuan(18));
//    }];
}

- (void)createUserView {
    
    __weak typeof(self) weakSelf = self;
    
    UIView *userView = [[UIView alloc] init];
    self.userView = userView;
    userView.backgroundColor = WhiteColor;
    [self.view addSubview:userView];
    [userView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.view.mas_top).with.offset(Margin);
        make.height.mas_equalTo(jisuan(66));
    }];
    
    UIImageView *userBgIV = [[UIImageView alloc] init];
    userBgIV.contentMode = UIViewContentModeScaleAspectFit;
    userBgIV.image = [UIImage imageNamed:@"userViewBG"];
    [userView addSubview:userBgIV];
    [userBgIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(userView.mas_centerX); make.centerY.mas_equalTo(userView.mas_centerY);
        make.width.mas_equalTo(jisuan(750));
        make.height.mas_equalTo(jisuan(66));
    }];
    
    UIImageView *iconIV = [[UIImageView alloc] init];
    self.iconIV = iconIV;
    iconIV.contentMode = UIViewContentModeScaleAspectFit;
    iconIV.layer.cornerRadius = jisuan(21);
    iconIV.layer.masksToBounds = YES;
    if ([CommonUserInfo sharedInstance].photoUrl.length <= 0) {
        iconIV.image = [UIImage imageNamed:@"buyMemberUserBG"];
    }else {
        [iconIV sd_setImageWithURL:[NSURL URLWithString:[CommonUserInfo sharedInstance].photoUrl]];
    }
    [userView addSubview:iconIV];
    [iconIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(userView.mas_left).with.offset(jisuan(40));
        make.centerY.mas_equalTo(userView.mas_centerY);
        make.width.mas_equalTo(jisuan(42));
        make.height.mas_equalTo(jisuan(42));
    }];
    
    UILabel *userNameL = [[UILabel alloc] init];
    self.userNameL = userNameL;
    userNameL.text = [CommonUserInfo sharedInstance].nickName;
    userNameL.textColor = Color333333;
    userNameL.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:jisuan(24)];
    userNameL.textAlignment = NSTextAlignmentLeft;
    [userView addSubview:userNameL];
    [userNameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(iconIV.mas_right).with.offset(Margin);
        make.centerY.mas_equalTo(userView.mas_centerY);
    }];
    
    UILabel *userPhoneL = [[UILabel alloc] init];
    userPhoneL.text = [NSString stringWithFormat:@"绑定手机：%@", [CommonUserInfo sharedInstance].phone];
    userPhoneL.textColor = Color999999;
    userPhoneL.font = Font(24);
    userPhoneL.textAlignment = NSTextAlignmentLeft;
    [userView addSubview:userPhoneL];
    [userPhoneL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(userNameL.mas_right).with.offset(Margin);
        make.centerY.mas_equalTo(userView.mas_centerY);
    }];
}

- (void)createMemberDesView {
    
    __weak typeof(self) weakSelf = self;
    
    UIView *memberDeslView = [[UIView alloc] init];
    self.memberDeslView = memberDeslView;
    memberDeslView.backgroundColor = WhiteColor;
    [self.view addSubview:memberDeslView];
    [memberDeslView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.userView.mas_bottom);
        make.height.mas_equalTo(jisuan(424));
    }];
    
    UIImageView *memberDesIV = [[UIImageView alloc] init];
    memberDesIV.contentMode = UIViewContentModeScaleAspectFit;
    memberDesIV.image = [UIImage imageNamed:@"memberDesViewbg"];
    [memberDeslView addSubview:memberDesIV];
    [memberDesIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(memberDeslView.mas_centerX);
        make.top.mas_equalTo(memberDeslView.mas_top).with.offset(jisuan(44));
        make.width.mas_equalTo(jisuan(590));
        make.height.mas_equalTo(jisuan(350));
    }];
    
    UILabel *levelL = [[UILabel alloc] init];
    self.levelL = levelL;
    levelL.text = @"VIP";
    levelL.textColor = Colorf8e3ac;
    levelL.font = Font(80);
    levelL.textAlignment = NSTextAlignmentLeft;
    [memberDeslView addSubview:levelL];
    [levelL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(memberDesIV.mas_left).with.offset(jisuan(50));
        make.top.mas_equalTo(memberDesIV.mas_top).with.offset(jisuan(50));
        make.width.mas_equalTo(jisuan(140));
        make.height.mas_equalTo(jisuan(80));
    }];
    
    UILabel *desL1 = [[UILabel alloc] init];
    desL1.text = @"爱淘帮会员，专属超值商品";
    desL1.textColor = Colorf8e3ac;
    desL1.font = Font(28);
    desL1.textAlignment = NSTextAlignmentLeft;
    [memberDeslView addSubview:desL1];
    [desL1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(levelL.mas_right).with.offset(jisuan(10));
        make.top.mas_equalTo(levelL.mas_top);
        make.right.mas_equalTo(memberDesIV.mas_right).with.offset(-jisuan(40));
        make.height.mas_equalTo(jisuan(28));
    }];
   
    UILabel *desL2 = [[UILabel alloc] init];
    self.desL2 = desL2;
//    desL2.text = @"会员趣豆抵扣可达80%～100%";
    desL2.textColor = Colorf8e3ac;
    desL2.font = Font(20);
    desL2.textAlignment = NSTextAlignmentLeft;
    [memberDeslView addSubview:desL2];
    [desL2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(levelL.mas_right).with.offset(jisuan(10));
        make.bottom.mas_equalTo(levelL.mas_bottom);
        make.right.mas_equalTo(memberDesIV.mas_right).with.offset(-jisuan(40));
        make.height.mas_equalTo(jisuan(28));
    }];
    
    UILabel *desL3 = [[UILabel alloc] init];
    self.desL3 = desL3;
    desL3.text = @"开通会员惊喜多多，优惠不断";
    desL3.textColor = Colorf8e3ac;
    desL3.numberOfLines = 2;
    desL3.font = Font(20);
    desL3.textAlignment = NSTextAlignmentLeft;
    [memberDeslView addSubview:desL3];
    [desL3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(memberDesIV.mas_left).with.offset(jisuan(50));
        make.top.mas_equalTo(memberDesIV.mas_top).with.offset(jisuan(196)); // 182
        make.right.mas_equalTo(memberDesIV.mas_right).with.offset(-jisuan(40));
    }];
    
}

- (NSMutableArray *)imageBgArr {
    
    if (!_imageBgArr) {
        
        _imageBgArr = [NSMutableArray array];
    }

    return _imageBgArr;
}
- (NSMutableArray *)moneyArr {
    
    if (!_moneyArr) {
        
        _moneyArr = [NSMutableArray array];
    }
    
    return _moneyArr;
}

#pragma mark -设置
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
//    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
